package com.example.sxt_practise.IO;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @author gaofeng
 * @date 2022年05月08日 16:28
 */
public class IoTest03 {
    public static void main(String[] args) {
        // 1.创建源
        File destDir = new File("dir/test2/song.txt");
        // 2.选择流
        OutputStream outputStream = null;
        try {
            // 追加
//            outputStream = new FileOutputStream(destDir, true);
            // 覆盖
            outputStream = new FileOutputStream(destDir, false);
            // 3. 操作,分段读取
//            byte[] flush = new byte[1024 * 2];
            byte[] flush = "oh my love,咱们结婚吧!".getBytes();
            outputStream.write(flush, 0, flush.length);
            outputStream.flush();
            // 关闭资源
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(outputStream)) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
